#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include "stack.h"
#include "error.h"
struct s_Stack
{
      arista* st;
      int ult;
      int max;
};

stack st_empty(int max)
{
      stack ret=NULL;
      ret=(stack)calloc(1,sizeof(struct s_Stack));
      if(ret == NULL)
      {
            warnx(ERRFMT,"st_empty: lack of memory",__LINE__);
            return NULL;
      }
      else
      {
            ret -> st=(arista*)calloc(max,sizeof(arista));
            if(ret -> st == NULL)
            {
                  warnx(ERRFMT,"st_empty: lack of memory",__LINE__);
                  free(ret);
                  return NULL;
            }
            else
            {
                  ret -> ult=0;
                  ret -> max=max;
                  return ret;
            }
      }
}

stack push(stack st, arista a)
{
      assert(st != NULL);
      assert(st -> ult < st -> max-1);/*si ult == max-1 entonces el stack est� lleno.*/
      st -> st[st -> ult] = arista_clone(a);
      st -> ult = st -> ult + 1;
      return st;
}

arista top(stack st)
{
      assert(st != NULL);
      assert(st -> ult > 0);
      return st -> st[st -> ult - 1];
}
stack pop(stack st)
{
      assert(st != NULL);
      st -> st[st -> ult - 1] = arista_destroy(st -> st[st -> ult - 1]);
      st -> ult = st -> ult - 1;
      return st;
}

Bool is_empty(stack st)
{
      return (st -> ult == 0);
}

stack destroy(stack st)
{
      if(st == NULL)
            return NULL;
      else
      {
            free(st -> st);
            free(st);
            st = NULL;
            return st;
      }
}

/*void print(stack st)
{
      int i;
      for(i=0;i<st -> ult;i++)
      {
            printf("-------------------");
            printf("\nPeso : %i",arista_peso(st -> st[i]));
            printf("\nv1 : %i",arista_v1(st -> st[i]));
            printf("\nv2 : %i",arista_v2(st -> st[i]));
            printf("\n");
            printf("-------------------");
      }
}*/
